﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Core
{
    public static partial class system
    {
        public static dynamic deleteLang(int code)
        {
            var sql = new Sql.SqlDataContext();
            var l = sql.Language.FirstOrDefault(row => row.code == code);
            if (l == null) return new
                {
                    success = false,
                    msg = "语言不存在"
                };

            // Check SpecialJudge
            var psps = sql.Problem.Where(row => row.specialLang == code).Select(row => row.pid);
            if (psps.Any())
            {
                var pstr = new StringBuilder();
                foreach (var pr in psps)
                {
                    if (pstr.Length != 0) pstr.Append(',');
                    pstr.Append(pr);
                }
                return new
                {
                    success = false,
                    msg = "无法删除语言，因为题目[" + pstr + "]以该语言作SpecialJudge评判程序"
                };
            }

            // Check Example
            var psms = sql.Problem.Where(row => row.exampleLang == code).Select(row => row.pid);
            if (psms.Any())
            {
                var pstr = new StringBuilder();
                foreach (var pr in psms)
                {
                    if (pstr.Length != 0) pstr.Append(',');
                    pstr.Append(pr);
                }
                return new
                    {
                        success = false,
                        msg = "无法删除语言，因为题目[" + pstr + "]以该语言作标程"
                    };
            }

            // Delete Status
            var sts = sql.Status.Where(row => row.lang == code);
            foreach (var s in sts)
            {
                if (s.status < 3 || s.god) continue;
                // Update PS
                var ps = sql.ProblemStatistics.FirstOrDefault(row => row.pid == s.pid);
                if (ps != null) problem.deleteProblemStatistics(ps, s.status);
                // Update US
                var us = sql.UserStatistics.FirstOrDefault(row => row.username == s.username);
                if (us != null) user.deleteUserStatistics(us, s.status);
                // Update Solve
                var so = sql.Solve.FirstOrDefault(row => row.username == s.username && row.pid == s.pid);
                if (so != null)
                {
                    --so.submit;
                    if (s.status == 3)
                    {
                        --so.accept;
                        if (so.bestRid == s.rid)
                        {
                            var newBest = (from s2 in sql.Status
                                           where s2.username == s.username && s2.pid == s.pid && s2.status == 3 && s2.rid != s.rid && !s2.god
                                           orderby s2.exeTime, s2.exeMem, s2.code.Length, s2.submitTime
                                           select s2).FirstOrDefault();
                            so.bestRid = newBest == null ? null : (int?)newBest.rid;
                        }
                    }
                }
            }
            sql.Status.DeleteAllOnSubmit(sts);

            // Delete Language
            sql.Language.DeleteOnSubmit(l);
            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }
    }
}
